package com.ztf.room.controller;

import com.github.pagehelper.PageInfo;
import com.ztf.room.bean.*;
import com.ztf.room.service.StudyRoomService;
import com.ztf.room.utils.Result;
import com.ztf.room.utils.ResultInfo;
import com.ztf.room.utils.SessionConstant;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;

@Controller
@RequestMapping("/studyRoom")
public class StudyRoomController {


    @Autowired
    private StudyRoomService studyRoomService;

    // 查询自习室状态
    @RequestMapping("/roomStatus")
    public String roomStatus(){
        return "student/studyroom.jsp";
    }
    // 填写申请自习室信息
    @RequestMapping("/apply")
    public String apply(){
        return "student/page/apply.jsp";
    }

    // 提交申请
    @RequestMapping("/saveApplyRoom")
    @ResponseBody
    public Result saveApplyRoom(RoomApplys roomApplys){
        System.out.println(roomApplys);
        boolean flag = studyRoomService.saveApplyRoom(roomApplys);
        if (!flag){
            return ResultInfo.err("申请失败");
        }
        return ResultInfo.success();
    }


    // 申请自习室记录
    @RequestMapping("/applyRoom")
    public String applyRoom(){
        return "student/applyRoom.html";
    }
    // 审核页面
    @RequestMapping("/roomAudit")
    public String roomAudit(){
        return "student/roomAudit.html";
    }

    // 获取自习室列表
    @RequestMapping("/getRoomList")
    @ResponseBody
    public Result getRoomList(int page,int limit){
        PageInfo<StudyRooms> pageInfo =  studyRoomService.getRoomList(page,limit);
        return ResultInfo.success(pageInfo);
    }


    // 学生获取申请的自习室
    @RequestMapping("/getApplyResultsByStu")
    @ResponseBody
    public Result getApplyResultsByStu(int page, int limit, HttpSession session){
        Student student = (Student) session.getAttribute(SessionConstant.STUDENT_NAME);
        // 登录的学生的学号
        PageInfo<RoomApplys> pageInfo =  studyRoomService.getApplyResultsByStu(student.getStudentId(),page,limit);
        return ResultInfo.success(pageInfo);
    }

    // 管理员查看当前提交申请自习室记录
    @RequestMapping("/getApplyResults")
    @ResponseBody
    public Result getApplyResults(int page,int limit){
        PageInfo<RoomApplys> pageInfo = studyRoomService.getApplyResults(page,limit);
        return ResultInfo.success(pageInfo);
    }

    // 管理员处理自习室申请
    @RequestMapping("/handleApply")
    @ResponseBody
    public Result handleApply(RoomApplyResults results,HttpSession session){
        RoomAdmin roomAdmin = (RoomAdmin) session.getAttribute(SessionConstant.ADMIN_NAME);
        boolean flag =  studyRoomService.handleAppy(results,roomAdmin);
        return ResultInfo.success("");
    }


}
